Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom logging provider to support structured logging for SLF4J 1.x + Logback 1.2.x #3693

Open
wants to merge 66 commits into
base: main
Choose a base branch
from

Conversation

JoeWang1127
Copy link
Collaborator

@JoeWang1127 JoeWang1127 commented Mar 12, 2025

In this PR:

  • Add Custom logging provider to support structured logging for SLF4J 1.x + Logback 1.2.x.
  • Add showcase tests.

b/394880750

@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label Mar 12, 2025
@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Mar 12, 2025
@JoeWang1127 JoeWang1127 changed the title feat: add sdk logging helpers feat: add customer logging provider to support structured logging for SLF4J 1.x + Logback 1.2.x Mar 13, 2025
@JoeWang1127 JoeWang1127 marked this pull request as ready for review March 18, 2025 17:26
for (Map.Entry<String, String> entry : mdcProperties.entrySet()) {
String fieldName = entry.getKey();
String entryValueString = entry.getValue();
if (fieldName == null || entryValueString == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logstash-logback has some customization capabilities that I suspect are fairly easy to restore here, like configure specific entries in the MDC to be included or excluded and renaming keys, ref: readme:mdc-fields. It might be as easy as adding these conditions here, or a bit more involved. Can you take a look? I think it nice to have it preserved.
https://github.com/logfellow/logstash-logback-encoder/blob/76f5295f35bc44518af91bfccb07a8ecb142229c/src/main/java/net/logstash/logback/composite/loggingevent/MdcJsonProvider.java#L102-L103

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added these features.

@JoeWang1127 JoeWang1127 requested a review from zhumin8 March 18, 2025 21:00
Copy link
Contributor

@zhumin8 zhumin8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Do you mind add a comment to pom about logstash.encoder.version used (7.3) so its easier to track in the future? Also add it to renovate config, I believe we do not want to this version upgraded?

String entryValueString = entry.getValue();
// an entry will be skipped if one of the scenario happens:
// 1. key or value is null
// 2. includeMdcKeyNames is not empty and the key is not in the list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: for readability, do you mind making this comment the same logic as code?

@JoeWang1127 JoeWang1127 changed the title feat: add customer logging provider to support structured logging for SLF4J 1.x + Logback 1.2.x feat: add custom logging provider to support structured logging for SLF4J 1.x + Logback 1.2.x Mar 19, 2025
@@ -228,6 +228,10 @@ jobs:
- name: Install Maven modules
run: |
mvn install -B -ntp -DskipTests -Dclirr.skip -Dcheckstyle.skip
- name: Install logging module # this module is not part of root project.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to install logging module in the main CI?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package com.google.cloud.sdk.logging;

import ch.qos.logback.classic.spi.ILoggingEvent;
import com.fasterxml.jackson.core.JsonGenerator;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is extending from MdcJsonProvider required? The writeTo interface exposes a jackson class JsonGenerator on the surface, and jackson is in general not recommended because of endless security issues.

Copy link
Collaborator Author

@JoeWang1127 JoeWang1127 Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The writeTo method is part of JsonProvider, which is implemented by MdcJsonProvider.

The JsonProvider is used in LogstashEncoder.

I don't think we can change the interface but we can extract jackson library and update to the lastest, what do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an interface that does not expose jackson on the surface? if not, I think we may need to re-evaluate the feasibility of it. Since adding a new dependency, especially jackson, is a big commitment.

Copy link

Copy link

Quality Gate Passed Quality Gate passed for 'java_showcase_integration_tests'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants